package com.secondboot.controller;

import com.secondboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname: UserController
 * @Author: bromide
 * @CreateTime: 2022/4/22--19:25
 * @Version: V1.0
 * @Description:
 */
@Controller
public class UserController {
    private static File userDir;
    static {
        userDir = new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }
    @RequestMapping("/myweb/reg")
    public void reg(String username,String password,String nickname,int age, HttpServletResponse response){
        File userFile = new File(userDir,username+".obj");
        if (userFile.exists()){
            //返回用户已存在页面
            try {
                response.sendRedirect("/reg_haveUser.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }


        try(
            FileOutputStream fos = new FileOutputStream(userFile);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        )
        {
            User user = new User(username,password,nickname,age);
            oos.writeObject(user);
            response.sendRedirect("/reg_success.html");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/myweb/login")
    public void login(String username,String password,HttpServletResponse response){
        File userFile = new File(userDir,username+".obj");
        if (!userFile.exists()){
            try {
                response.sendRedirect("/login_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try(
            FileInputStream fis = new FileInputStream(userFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
        )
        {
            User user = (User)ois.readObject();
            if (user.getPassword().equals(password)){
                response.sendRedirect("/login_success.html");
            }else {
                response.sendRedirect("/login_fail.html");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/myweb/showAllUser")
    public void showAllUser(HttpServletRequest request,HttpServletResponse response){
        //先将文件夹下的所有用户信息读取出来备用
        List<User> userList = new ArrayList<>();
        //为防止文件夹下有隐藏文件，所以需要筛选。
        File[] files = userDir.listFiles(file -> file.getName().endsWith(".obj"));
        for (File file : files){
            try(
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
            )
            {
                User user = (User)ois.readObject();
                userList.add(user);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //设置返回值类型   不然会乱码
        response.setContentType("text/html;charset=utf-8");

        //写入动态文件
        try {
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户展示</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户展示</h1>");
            pw.println("<form action=\"/myweb/showAllUser\" method=\"post\">");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("<td>操作</td>");
            pw.println("</tr>");
            for(User user : userList){
                pw.println("<tr>");
                pw.println("<td>"+user.getUsername()+"</td>");
                pw.println("<td>"+user.getPassword()+"</td>");
                pw.println("<td>"+user.getNickname()+"</td>");
                pw.println("<td>"+user.getAge()+"</td>");
                pw.println("<td><a href=\"/myweb/deleteUser?username="+user.getUsername()+"\">删除</a></td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</form>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @RequestMapping("/myweb/deleteUser")
    public void deleteUser(String username,HttpServletResponse response){
        File userFile = new File(userDir,username+".obj");
        userFile.delete();
        try {
            response.sendRedirect("/myweb/showAllUser");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
